package com.bawei.a2010douyin.widget;

import android.graphics.Rect;
import android.view.View;

import androidx.recyclerview.widget.RecyclerView;

public class RecyclerViewItemDecoration extends RecyclerView.ItemDecoration {
    private int space;//分割线间隙
    private int spanCount;//列数
    //构造方法
    public RecyclerViewItemDecoration(int space,int spanCount) {
        this.space = space;
        this.spanCount = spanCount;
    }
    //重写此方法：outRect当前view的外围
    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        outRect.top = space;
        //该View在整个RecyclerView中位置。
        int pos = parent.getChildAdapterPosition(view);
        if (pos % spanCount == 0) {       //判断是否为最左边一列
            outRect.left = 0;
            outRect.right = space/2;
        }else  if (pos % spanCount == spanCount-1) {//判断是否为最右边一列
            outRect.left = space/2;
            outRect.right = 0;
        }else{//中间
            outRect.left = space/2;
            outRect.right = space/2;
        }
    }
}
